home *** CD-ROM | disk | FTP | other *** search
/ s-gikan2.maizuru-ct.ac.jp / s-gikan2.maizuru-ct.ac.jp.zip / s-gikan2.maizuru-ct.ac.jp / pub / ncvc352a_install.exe / {app} / scripts / control_cutter_direction.pl < prev    next >
Perl Script  |  2004-04-02  |  9KB  |  324 lines

  1. #! /usr/bin/perl
  2.  
  3. #  ZÄ▓ò√îⁿé╔É╪ìφæùéΦé┼ë║ì~é╡üA                                               #
  4. #  Éàò╜ò√îⁿé╔Æ╝ÉⁿòΓè╘é┼Éié▐ÅΩìçüAë║ì~é╠Æ╝æOé╔Éié▐ò√îⁿé╔ìHï∩é╠îⁿé½é≡ÅCÉ│é╖éΘ  #
  5. #  ë~î╩òΓè╘é┼Éié▐ÅΩìçüAÉnɵé≡û@Éⁿò√îⁿé╔é▐é»é╚é¬éτÉié▐û╜ù▀é≡òtë┴é╖éΘ          #
  6. #  ÿAæ▒é╡é╜òΓè╘é╠ÅΩìçüAìHï∩é╠îⁿé½é╠ÅCÉ│é¬òKùvé╚ÅΩìçé═êΩôxRô_é╔ò£ïAé╡é─       #
  7. #  Äƒé╠ò√îⁿé╔ìHï∩é╠îⁿé½é≡ÅCÉ│é╡é╜îπüAì─é╤ìHï∩é≡ë║éδé╡òΓè╘é≡æ▒ìsé╖éΘ          #
  8. #  ë~î╩òΓè╘îπüAăé╠ìsé¬ë~î╩òΓè╘é┼é╚éóÅΩìçüAû@Éⁿò√îⁿé≡âLâââôâZâïé╖éΘ          #
  9. #  é╞éóéñû╜ù▀é≡òtë┴é╖éΘâXâNâèâvâg                                            #
  10.  
  11. #  ÉΓæ╬ì└òWîn(G90)é╠é▌æ╬ë₧                                                   #
  12.  
  13. $pre_file= $ARGV[0];
  14. $out_file= $ARGV[1];
  15.  
  16. open(IN,$pre_file);
  17. @line= <IN>;
  18. close(IN);
  19.  
  20. open(OUT,">$out_file");
  21.  
  22. %XYZ= ("X",0,"Y",1,"Z",2,"I",0,"J",1);
  23. $PI= 3.1415926535897932;
  24. $RAD= 180/$PI;
  25. $cutter_direction= -1;
  26.  
  27. for($i=0;$i<=$#line;$i++){
  28.     $_= $line[$i];
  29.  
  30.     if(/^N?[\d]*[\(\%]/){ print OUT; }
  31.     else{
  32.         if(/G0*?([0123])[A-Z\s]/){ $G0X= $1; }
  33.         elsif(/G8[0-9][A-Z\s]/){ $G0X= 9; }
  34.  
  35.         if($housenMode == 1){
  36.             if($G0X !~ /[23]/){
  37.                 $housenMode= 0;
  38.                 print OUT "G40.1\n";
  39.             }
  40.         }
  41.         if($G0X == 1 and /Z([^A-Z\s]+)/){
  42.             $pointR= $abso[2];
  43.             $abso[2]= $1;
  44.             $kirikomiMode= 1;
  45.             $FxyMode= 0;
  46.             if(/F([^A-Z\s]+)/){ $okuriZ= $1; }
  47.         
  48.             $next_line= $line[$i+1];
  49.             if($next_line =~ /G0*?([0123])[A-Z\s]/){ $G0X= $1; }
  50.             if($next_line =~ /G8[0-9]/){ $G0X= 9; }
  51.  
  52.             if($next_line =~ /[XY]/ and $G0X == 1){
  53.                 if($next_line =~ /F([^A-Z\s]+)/){
  54.                     $okuriXY= $1;
  55.                     if($okuriZ != $okuriXY){ $FxyMode= 1; }
  56.                 }
  57.                 ($incli[0],$incli[1])= (0,0);
  58.                 while($next_line =~ /([XY])([^A-Z\s]+)/g){
  59.                     ($char,$num)= ($1,$2);
  60.                     $incli[$XYZ{$char}]= marume($num,$abso[$XYZ{$char}],'-');
  61.                     $abso[$XYZ{$char}]= $num;
  62.                 }
  63.                 $angle= get_angle(-$incli[1],$incli[0]);
  64.                 if($cutter_direction != $angle){ print OUT "C".$angle."\n"; }
  65.                 print OUT "M3\n";
  66.                 print OUT;
  67.                 print OUT $next_line;
  68.                 $i++;
  69.             }
  70.             elsif($G0X =~ /[23]/){
  71.                 $housenMode= 1;
  72.                 if($next_line =~ /F([^A-Z\s]+)/){
  73.                     $okuriXY= $1;
  74.                     if($okuriZ != $okuriXY){ $FxyMode= 1; }
  75.                 }
  76.                 ($center[0],$center[1])= (0,0);
  77.                 if($next_line =~ /R([^A-Z\s]+)/){
  78.                     $radius= $1;
  79.                     ($destination[0],$destination[1])= (0,0);
  80.                     while($next_line =~ /([XY])([^A-Z\s]+)/g){
  81.                         $destination[$XYZ{$1}]= marume($2,$abso[$XYZ{$1}],'-');  #ê┌ô«æOé╠ô_é⌐éτê┌ô«îπé╠ô_é▄é┼é╠æèæ╬ì└òW
  82.                         $abso[$XYZ{$1}]= $2;
  83.                     }
  84.                     if($G0X == 2) {
  85.                         @center= get_center($destination[0],$destination[1],$radius);  #ê┌ô«æOé╠ô_é⌐éτë~î╩é╠ÆåÉSé▄é┼é╠æèæ╬ì└òW
  86.                     }
  87.                     else{
  88.                         @center= get_center($destination[0],$destination[1],-$radius);
  89.                     }
  90.                 }
  91.                 else{
  92.                     while($next_line =~ /([IJ])([^A-Z\s]+)/g){
  93.                         $center[$XYZ{$1}]= $2;
  94.                     }
  95.                 }
  96.                 if($G0X == 2){ $angle= get_angle(-$center[0],-$center[1]); }
  97.                 else{ $angle= get_angle($center[0],$center[1]); }
  98.                 if($cutter_direction != $angle){ print OUT "C".$angle."\n"; }
  99.                 print OUT "M3\n";
  100.                 print OUT;
  101.                 if($G0X == 2){
  102.                     $kaitenHoukou= 2;
  103.                     print OUT "G41.1";
  104.                 }
  105.                 else{
  106.                     $kaitenHoukou= 3;
  107.                     print OUT "G42.1";
  108.                 }
  109.                 print OUT $next_line;
  110.                 
  111.                 if($next_line =~ /R[^A-Z\s]+/){
  112.                     $center[0]= marume($center[0],$destination[0],'-');  #ê┌ô«îπé╠ô_é⌐éτë~î╩é╠ÆåÉSé▄é┼é╠æèæ╬ì└òW
  113.                     $center[1]= marume($center[1],$destination[1],'-');
  114.                 }
  115.                 else{
  116.                     while($next_line =~ /([XY])([^A-Z\s]+)/g){
  117.                         $incli_sub= marume($abso[$XYZ{$1}],$2,'-');  #ê┌ô«îπé╠ô_é⌐éτê┌ô«æOé╠ô_é▄é┼é╠æèæ╬ì└òW
  118.                         $center[$XYZ{$1}]= marume($center[$XYZ{$1}],$incli_sub);  #ê┌ô«îπé╠ô_é⌐éτÆåÉSé▄é┼é╠æèæ╬ì└òW
  119.                         $abso[$XYZ{$1}]= $2;
  120.                     }
  121.                 }
  122.                 
  123.                 if($G0X == 2){ $cutter_direction= get_angle(-$center[0],-$center[1]); }  #ê┌ô«îπé╠ô_é┼é╠âJâbâ^ü[é╠èpôx
  124.                 else{ $cutter_direction= get_angle($center[0],$center[1]); }
  125.                 $i++;
  126.             }
  127.             else{ print OUT; }
  128.         }
  129.         elsif($G0X == 1 and $kirikomiMode == 1 and /[XY]/){
  130.             ($incli[0],$incli[1])= (0,0);
  131.             while(/([XY])([^A-Z\s]+)/g){
  132.                 ($char,$num)= ($1,$2);
  133.                 $incli[$XYZ{$char}]= marume($num,$abso[$XYZ{$char}],'-');
  134.                 $abso[$XYZ{$char}]= $num;
  135.             }
  136.             $angle= get_angle(-$incli[1],$incli[0]);
  137.             if($cutter_direction != $angle){
  138.                 print OUT "G00Z".$pointR."\n";
  139.                 print OUT "M5\nC".$angle."\nM3\n";
  140.                 if($FxyMode == 1){
  141.                     print OUT "G01Z".$abso[2]."F".$okuriZ."\n";
  142.                     if(!/F([^A-Z\s]+)/){
  143.                         if(/\s*$/){
  144.                             print OUT $`."F".$okuriXY.$&;
  145.                         }
  146.                     }
  147.                     else{ print OUT; }
  148.                 }
  149.                 else{
  150.                     print OUT "G01Z".$abso[2]."\n";
  151.                     print OUT;
  152.                 }
  153.                 $cutter_direction= $angle;
  154.             }
  155.             else{ print OUT; }
  156.         }
  157.         elsif($G0X =~ /[23]/ and $kirikomiMode == 1){
  158.             ($center[0],$center[1])= (0,0);
  159.             if(/R([^A-Z\s]+)/){
  160.                 $radius= $1;
  161.                 ($destination[0],$destination[1])= (0,0);
  162.                 while(/([XY])([^A-Z\s]+)/g){
  163.                     $destination[$XYZ{$1}]= marume($2,$abso[$XYZ{$1}],'-');
  164.                     $abso[$XYZ{$1}]= $2;
  165.                 }
  166.                 if($G0X == 2) {
  167.                     @center= get_center($destination[0],$destination[1],$radius);
  168.                 }
  169.                 else{
  170.                     @center= get_center($destination[0],$destination[1],-$radius);
  171.                 }
  172.             }
  173.             else{
  174.                 while(/([IJ])([^A-Z\s]+)/g){
  175.                     $center[$XYZ{$1}]= $2;
  176.                 }
  177.             }
  178.             if($G0X == 2){ $angle= get_angle(-$center[0],-$center[1]); }
  179.             else{ $angle= get_angle($center[0],$center[1]); }
  180.  
  181.             if($cutter_direction != $angle){
  182.                 if($housenMode == 1){ print OUT "G40.1\n"; }
  183.                 print OUT "G00Z".$pointR."\n";
  184.                 print OUT "M5\nC".$angle."\nM3\n";
  185.                 if($FxyMode == 1){
  186.                     print OUT "G01Z".$abso[2]."F".$okuriZ."\n";
  187.                     if($G0X == 2){
  188.                         $kaitenHoukou= 2;
  189.                         print OUT "G41.1\n";
  190.                         if($housenMode == 1 and !/G0*?2[A-Z\s]/){ print OUT "G02"; }
  191.                     }
  192.                     if($G0X == 3){
  193.                         $kaitenHoukou= 3;
  194.                         print OUT "G42.1\n";
  195.                         if($housenMode == 1 and !/G0*?3[A-Z\s]/){ print OUT "G03"; }
  196.                     }
  197.                     if(!/F([^A-Z\s]+)/){
  198.                         if(/\s*$/){
  199.                             print OUT $`."F".$okuriXY.$&;
  200.                         }
  201.                     }
  202.                     else{ print OUT; }
  203.                 }
  204.                 else{
  205.                     print OUT "G01Z".$abso[2]."\n";
  206.                     if($G0X == 2){
  207.                         $kaitenHoukou= 2;
  208.                         print OUT "G41.1\n";
  209.                         if($housenMode == 1 and !/G0*?2[A-Z\s]/){ print OUT "G02"; }
  210.                     }
  211.                     if($G0X == 3){
  212.                         $kaitenHoukou= 3;
  213.                         print OUT "G42.1\n";
  214.                         if($housenMode == 1 and !/G0*?3[A-Z\s]/){ print OUT "G03"; }
  215.                     }
  216.                     print OUT;
  217.                 }
  218.             }
  219.             else{
  220.                 if($housenMode == 1){
  221.                     if($G0X == 2 and $kaitenHoukou == 3){
  222.                         $kaitenHoukou= 2;
  223.                         print OUT "G41.1\n";
  224.                     }
  225.                     elsif($G0X == 3 and $kaitenHoukou == 2){
  226.                         $kaitenHoukou= 3;
  227.                         print OUT "G42.1\n";
  228.                     }
  229.                 }
  230.                 else{
  231.                     if($G0X == 2){
  232.                         $kaitenHoukou= 2;
  233.                         print OUT "G41.1\n";
  234.                     }
  235.                     else{
  236.                         $kaitenHoukou= 3;
  237.                         print OUT "G42.1\n";
  238.                     }
  239.                 }
  240.                 print OUT;
  241.             }
  242.                 
  243.             if(/R[^A-Z\s]+/){
  244.                 $center[0]= marume($center[0],$destination[0],'-');
  245.                 $center[1]= marume($center[1],$destination[1],'-');
  246.             }
  247.             else{
  248.                 while(/([XY])([^A-Z\s]+)/g){
  249.                     $incli_sub= marume($abso[$XYZ{$1}],$2,'-');
  250.                     $center[$XYZ{$1}]= marume($center[$XYZ{$1}],$incli_sub);
  251.                     $abso[$XYZ{$1}]= $2;
  252.                 }
  253.             }
  254.  
  255.             if($G0X == 2){ $cutter_direction= get_angle(-$center[0],-$center[1]); }
  256.             else{ $cutter_direction= get_angle($center[0],$center[1]); }
  257.             $housenMode= 1;
  258.         }
  259.         elsif($G0X == 0 and /Z([^A-Z\s]+)/){
  260.             $pointZ= $1;
  261.             if($kirikomiMode == 1 and $pointZ > $abso[2]){
  262.                 $kirikomiMode= 0;
  263.                 print OUT;
  264.                 print OUT "M5\n";
  265.             }
  266.             else{ print OUT; }
  267.             $abso[2]= $pointZ;
  268.         }
  269.         else{
  270.             print OUT;
  271.             while(/([XYZ])([^A-Z\s]+)/g){
  272.                 $abso[$XYZ{$1}]= $2;
  273.             }
  274.         }
  275.     }
  276. }
  277.  
  278. close(OUT);
  279.  
  280.  
  281. sub marume{
  282.     my ($num1,$num2,$enzanshi)= @_;
  283.     my $num;
  284.         
  285.     $num1= int($num1*1000);
  286.     $num2= int($num2*1000);
  287.     if($enzanshi eq '-'){ $num= ($num1 - $num2) / 1000; }
  288.     else{ $num= ($num1 + $num2) / 1000; }
  289.     if($num !~ /\./ and $num != 0){ $num= $num.'.'; }
  290.     return $num;
  291.  
  292. sub marume2{
  293.     my ($num)= @_;
  294.     $num= int($num * 1000);
  295.     $num= $num / 1000;
  296.     if($num !~ /\./ and $num != 0){ $num= $num.'.'; }
  297.     return $num;
  298. }
  299.  
  300. sub get_angle{
  301.     my ($x,$y)= @_;
  302.     my $theta= atan2($y,$x)*$RAD;
  303.     if($y < 0){ $theta += 360; }
  304.     $theta= marume2($theta);
  305.     return $theta;
  306. }
  307.  
  308. sub get_center{
  309.     my ($x,$y,$r)= @_;
  310.     my (@center,$fugou);
  311.     $x= int($x * 1000);
  312.     $y= int($y * 1000);
  313.     $r= int($r * 1000);
  314.     my $obj= sqrt(4*$r*$r/($x*$x+$y*$y) - 1);
  315.     if($r > 0){ $fugou= 1; }
  316.     else{ $fugou= -1; }
  317.     $center[0]= ($x + $fugou*$y*$obj)/2000;
  318.     $center[1]= ($y - $fugou*$x*$obj)/2000;
  319.     $center[0]= marume2($center[0]);
  320.     $center[1]= marume2($center[1]);
  321.     return @center;
  322. }
  323.